//
//  iconChangeViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/22.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "iconChangeViewController.h"

@interface iconChangeViewController ()
@property (nonatomic, strong) NSMutableArray <NSString *>*iconNameArray;
@end

@implementation iconChangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.iconNameArray = [[NSMutableArray alloc]init];
    self.iconNameArray = @[@"icon1",@"icon2",@"icon3",@"icon4",@"icon15",@""].mutableCopy;
    //具体实现可以通过网络请求接口 进行判断
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeIcon)];
    [self.view addGestureRecognizer:tap];
    
}

- (void)changeIcon{
    NSString *iconName = self.iconNameArray[arc4random() % self.iconNameArray.count];
    [self setAppIconImageWithName:iconName];
    
}

#pragma mark - 设置icon
- (void)setAppIconImageWithName:(NSString *)iconName{
    if(![[UIApplication sharedApplication] supportsAlternateIcons]){
        return;
    }
    //防止没有icon name
    if([iconName isEqualToString:@""]){
        iconName = nil;
    }
    
    [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
        if(error){
            //没有设置配置文件时，不能更换
            NSLog(@"更换icon出错了！");
        }else{
            NSLog(@"icon 已被更换");
        }
    }];
    
}


@end
